using System;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Web.UI;
using Sedna.Core.Controller;
using Sedna.Core.Controls.Support;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	public class DateTimeValidator : System.Web.UI.WebControls.BaseValidator, IContent, IConfigurationAware
	{

		private IConfiguration configuration;
		private string configurationId="configuration.default.DateTimeValidator";

		private String contentPlaceHolderId;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { configuration = value; }
		}

		public string ConfigurationId
		{
			get { return configurationId; }
			set { configurationId = value; }
		}

		protected override bool EvaluateIsValid()
		{
			string controlValue=GetControlValidationValue(ControlToValidate);
			try 
			{
				DateTime.Parse(controlValue, Configuration.PrincipalWrapper.Culture.DateTimeFormat);
				return true;
			}
			catch
			{
				return false;	
			}
		}

		protected override void OnInit(EventArgs e)
		{
			Configuration=(IConfiguration)SpringContextFactory.GetContext().GetObject(ConfigurationId);
			Initialize();
			base.OnInit (e);
		}
/*
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);
			if (base.RenderUplevel)
			{
				writer.AddAttribute("evaluationfunction", "RegularExpressionValidatorEvaluateIsValid");
				writer.AddAttribute("validationexpression", GetDateTimeValidationExpression());
			}

		}

		private string GetDateTimeValidationExpression()
		{
			DateTimeFormatInfo dateFormat=Configuration.PrincipalWrapper.Culture.DateTimeFormat;
			StringBuilder expression=new StringBuilder(dateFormat.ShortDatePattern, dateFormat.ShortDatePattern.Length*2);
			
			expression=expression.Replace(".", "\\.");
			expression=expression.Replace("\\", "\\\\");

			string yyyy="([0-9]{4})";
			string yy="([0-9]{2})";
			string y="([1-9]|([1-9][0-9]))";
			string MMMM="("+JavascriptUtil.Serialize(dateFormat.MonthNames, "|")+")";
			string MMM="("+JavascriptUtil.Serialize(dateFormat.AbbreviatedMonthNames, "|")+")";
			string MM="((0[1-9])|(1[0-2]))";
			string M="([1-9]|(1[0-2]))";
			string dddd="("+JavascriptUtil.Serialize(dateFormat.DayNames, "|")+")";
			string ddd="("+JavascriptUtil.Serialize(dateFormat.DayNames, "|")+")";
			string dd="((0[1-9])|([12][0-9])|(3[01]))";
			string d="(([1-9])|([12][0-9])|(3[01]))";

		
			expression=expression.Replace("yyyy", yyyy);
			expression=expression.Replace("yy", yy);
			expression=expression.Replace("y", y);
			expression=expression.Replace("MMMM", MMMM);
			expression=expression.Replace("MMM", MMM);
			expression=expression.Replace("MM", MM);
			expression=expression.Replace("M", M);
			expression=expression.Replace("dddd", dddd);
			expression=expression.Replace("ddd", ddd);
			expression=expression.Replace("dd", dd);
			expression=expression.Replace("d", d);

			

			expression=expression.Replace("/", dateFormat.DateSeparator);
				
			return "^"+expression.ToString()+"$";
		}
*/
		#region Localization Logic

		private string textKey;
		private string errorMessageKey;

		public void Initialize()
		{
			base.Text = ControlUtil.ExtractMessageManager(this).GetMessage(textKey!=null&&textKey!=string.Empty?textKey:(this.ID+".textKey"), base.Text);
			base.ErrorMessage = ControlUtil.ExtractMessageManager(this).GetMessage(errorMessageKey!=null&&errorMessageKey!=string.Empty?errorMessageKey:(this.ID+".errorMessage"), base.ErrorMessage);
		}


		public string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public string ErrorMessageKey
		{
			get { return errorMessageKey; }
			set { errorMessageKey = value; }
		}

		#endregion Localization Logic
	}
}
